# coding=utf-8
# Date: 2021/9/25 10:58


# 执行用时：260 ms, 在所有 Python3 提交中击败了30.71%的用户
# 内存消耗：18.5 MB, 在所有 Python3 提交中击败了33.08%的用户
class Solution:
    def minDistance(self, a: str, b: str) -> int:
        n, m = len(a), len(b)
        f = [[0 for _ in range(m + 1)] for _ in range(n + 1)]

        for i in range(n + 1):
            f[i][0] = i
        for i in range(m + 1):
            f[0][i] = i
        for i in range(1, n + 1):
            for j in range(1, m + 1):
                f[i][j] = min(f[i - 1][j], f[i][j - 1]) + 1
                if a[i - 1] == b[j - 1]:
                    f[i][j] = min(f[i][j], f[i - 1][j - 1])
        return f[n][m]


if __name__ == "__main__":
    print(Solution().minDistance("sea", "eat"))  # 2
